﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThietBiSo.Helper;
using ThietBiSo.LinQDB;


namespace ThietBiSo.Controllers
{
    public abstract class ApplicationController : ApplicationBaseController
    {

        public int start;
        public int offset;        //


        // Pagination on list
        public void PagingController(string url, int total, int page, string param)
        {
            Pagination pagination = new Pagination(true);
            pagination.BaseUrl = GetAppPath() + url;
            pagination.TotalRows = total;
            pagination.CurPage = page;
            pagination.PerPage = 10;
            pagination.Params = param;

            pagination.PrevLink = "Prev";
            pagination.NextLink = "Next";

            string pageLinks = pagination.GetPageLinks();

            start = (page - 1) * pagination.PerPage;
            offset = pagination.PerPage;
            ViewData["total"] = total;
            ViewData["pageLinks"] = pageLinks;
        }
        public void PagingControllerSanPham(string url, int total, int page, string param)
        {
            Pagination pagination = new Pagination(true);
            pagination.BaseUrl = GetAppPath() + url;
            pagination.TotalRows = total;
            pagination.CurPage = page;
            pagination.PerPage = 18;
            pagination.Params = param;

            pagination.PrevLink = "Prev";
            pagination.NextLink = "Next";

            string pageLinks = pagination.GetPageLinks();

            start = (page - 1) * pagination.PerPage;
            offset = pagination.PerPage;
            ViewData["total"] = total;
            ViewData["pageLinks"] = pageLinks;
        }
        public void PagingControllerReadmore(string url, int total, int page, string param)
        {
            Pagination pagination = new Pagination(true);
            pagination.BaseUrl = GetAppPath() + url;
            pagination.TotalRows = total;
            pagination.CurPage = page;
            pagination.PerPage = 6;
            pagination.Params = param;

            pagination.PrevLink = "Prev";
            pagination.NextLink = "Next";

            string pageLinks = pagination.GetPageLinks();

            start = (page - 1) * pagination.PerPage;
            offset = pagination.PerPage;
            ViewData["total"] = total;
            ViewData["pageLinks"] = pageLinks;
        }
      
        public Admin GetAdmin()
        {
            Admin user = new Admin();
            if (Session["HoTenAdmin"] != null)
            {
                user = (Admin)Session["HoTenAdmin"];

                return user;
            }
            return null;
        }

    }

    public abstract class ApplicationBaseController : Controller
    {
        private string appPath;

        // LẤY ĐƯỜNG DẪN CỦA HỆ THỐNG
        public string GetAppPath()
        {
            appPath = System.Web.HttpContext.Current.Request.ApplicationPath;
            if (appPath == string.Empty || appPath.Equals("/")) appPath = "";
            return appPath;
        }

        protected override void OnException(ExceptionContext filterContext)
        {
            if (filterContext.Exception != null)
            {
                if (filterContext.Exception.Message.ToLower().Contains("object reference not set to an instance of an object."))
                {
                    RedirectToAction("LogOn?errTimeOut=true", "Home");
                }
            }
            base.OnException(filterContext);

        }
    }


}
